package com.fz.us.oa.core.dao.com.impl;

import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.result.SearchResultList;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.company.dao.impl.BaseFlowDaoImpl;
import com.fz.us.admin.company.service.UserService;
import com.fz.us.admin.dict.entity.Dict;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.admin.dict.service.DictItemService;
import com.fz.us.admin.dict.service.DictService;
import com.fz.us.base.bean.BaseEnum;
import com.fz.us.oa.core.dao.com.PersonDao;
import com.fz.us.oa.core.entity.com.Person;
import com.fz.us.oa.core.service.com.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dzw on 2018/3/13.
 */
@Repository
public class PersonDaoImpl extends BaseFlowDaoImpl<Person, String> implements PersonDao {

    @Autowired
    private UserService userService;
    @Autowired
    private PersonService personService;
    @Autowired
    private DictService dictService;
    @Autowired
    private DictItemService dictItemService;

    //获取换届前系统字典（2015-2018）
    public DictItem getGeneralBefore(){
        DictItem generalBefore = new DictItem();
        Dict generalDict = dictService.getByValue("general");
        if(generalDict!=null){
            generalBefore = dictItemService.getByValue(generalDict.getId(),"general1"); //换届前（2015-2018）
        }
        return generalBefore;
    }

    //获取换届前系统字典（2015-2018）
    public DictItem getGeneralAfter(){
        DictItem generalAfter = new DictItem();
        Dict generalDict = dictService.getByValue("general");
        if(generalDict!=null){
            generalAfter = dictItemService.getByValue(generalDict.getId(),"general2"); //换届后（2018-2021）
        }
        return generalAfter;
    }

    @Override
    public int getAllCount(DictItem general) {
        Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.generalDict=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, general)
                .uniqueResult();
        return count==null?0:Integer.parseInt(count.toString());
    }

    @Override
    public int getResidentCountByStreet(DictItem street) {
        Object count = getSession().createQuery("select count(*) from Resident re where re.state=? and re.street=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, street)
                .uniqueResult();
        return count==null?0:Integer.parseInt(count.toString());
    }

    @Override
    public List<Person> findPerson(List<Rule> rules) {
        if(rules==null){
            rules = new ArrayList<Rule>();
        }
        Order order = Order.order("pinYinHead", SearchEnum.OrderType.asc);

        rules.add(RuleHelper.eq("state", BaseEnum.StateEnum.Enable));

        SearchResultList searchResult = personService.findListBySearch(rules, order);
        List<Person> personList = searchResult.getList();
        return personList;
    }

    @Override
    public int getCountEduAll(boolean isBeiXuan, DictItem beiXuan, DictItem edu, DictItem general) {
        if(isBeiXuan){
            Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature=? and p.highEdu=? and p.generalDict=?")
                    .setParameter(0, BaseEnum.StateEnum.Enable)
                    .setParameter(1, beiXuan)
                    .setParameter(2, edu)
                    .setParameter(3, general)
                    .uniqueResult();
            return count!=null ? Integer.parseInt(count.toString()) : 0;
        }else{
            Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature!=? and p.highEdu=? and p.generalDict=?")
                    .setParameter(0, BaseEnum.StateEnum.Enable)
                    .setParameter(1, beiXuan)
                    .setParameter(2, edu)
                    .setParameter(3, general)
                    .uniqueResult();
            return count!=null ? Integer.parseInt(count.toString()) : 0;
        }

    }

    @Override
    public int getCountEduAll(boolean isBeiXuan, DictItem beiXuan, DictItem edu, DictItem street, DictItem general) {
        if(isBeiXuan){
            Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature=? and p.highEdu=? and p.street=? and p.generalDict=?")
                    .setParameter(0, BaseEnum.StateEnum.Enable)
                    .setParameter(1, beiXuan)
                    .setParameter(2, edu)
                    .setParameter(3, street)
                    .setParameter(4, general)
                    .uniqueResult();
            return count!=null ? Integer.parseInt(count.toString()) : 0;
        }else{
            Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature!=? and p.highEdu=? and p.street=? and p.generalDict=?")
                    .setParameter(0, BaseEnum.StateEnum.Enable)
                    .setParameter(1, beiXuan)
                    .setParameter(2, edu)
                    .setParameter(3, street)
                    .setParameter(4, general)
                    .uniqueResult();
            return count!=null ? Integer.parseInt(count.toString()) : 0;
        }
    }

    @Override
    public int getCountEduAll(DictItem edu, DictItem general) {
        Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.highEdu=? and p.generalDict=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, edu)
                .setParameter(2, general)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public int getCountEduAll(DictItem edu, DictItem street, DictItem general) {
        Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.highEdu=? and p.street=? and p.generalDict=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, edu)
                .setParameter(2, street)
                .setParameter(3, general)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public int getBeiXuanCount(boolean isBeiXuan, DictItem beiXuan, DictItem general) {
        if(isBeiXuan){
            Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature=? and p.generalDict=?")
                    .setParameter(0, BaseEnum.StateEnum.Enable)
                    .setParameter(1, beiXuan)
                    .setParameter(2, general)
                    .uniqueResult();
            return count!=null ? Integer.parseInt(count.toString()) : 0;
        }else{
            Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature!=? and p.generalDict=?")
                    .setParameter(0, BaseEnum.StateEnum.Enable)
                    .setParameter(1, beiXuan)
                    .setParameter(2, general)
                    .uniqueResult();
            return count!=null ? Integer.parseInt(count.toString()) : 0;
        }

    }

    @Override
    public int getCountByPersonNature(DictItem personNature, DictItem general) {
        Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature=? and p.generalDict=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, personNature)
                .setParameter(2, general)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public int getCountByNatureAndStreet(DictItem personNature, DictItem street, DictItem general) {
        Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature=? and p.street=? and p.generalDict=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, personNature)
                .setParameter(2, street)
                .setParameter(3, general)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public int getCountByStreet(DictItem street, DictItem general) {
        Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.street=? and p.generalDict=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, street)
                .setParameter(2, general)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public int getCountByGenderAndBeiXuan(boolean isBeiXuan, DictItem beiXuan, String gender, DictItem general) {
        if(isBeiXuan){
            Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature=? and p.gender=? and p.generalDict=?")
                    .setParameter(0, BaseEnum.StateEnum.Enable)
                    .setParameter(1, beiXuan)
                    .setParameter(2, gender)
                    .setParameter(3, general)
                    .uniqueResult();
            return count!=null ? Integer.parseInt(count.toString()) : 0;
        }else{
            Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature!=? and p.gender=? and p.generalDict=?")
                    .setParameter(0, BaseEnum.StateEnum.Enable)
                    .setParameter(1, beiXuan)
                    .setParameter(2, gender)
                    .setParameter(3, general)
                    .uniqueResult();
            return count!=null ? Integer.parseInt(count.toString()) : 0;
        }

    }

    @Override
    public int getCountByGenderAndBeiXuan(boolean isBeiXuan, DictItem beiXuan, String gender, DictItem street, DictItem general) {
        if(isBeiXuan){
            Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature=? and p.gender=? and p.street=? and p.generalDict=?")
                    .setParameter(0, BaseEnum.StateEnum.Enable)
                    .setParameter(1, beiXuan)
                    .setParameter(2, gender)
                    .setParameter(3, street)
                    .setParameter(4, general)
                    .uniqueResult();
            return count!=null ? Integer.parseInt(count.toString()) : 0;
        }else{
            Object count = getSession().createQuery("select count(*) from Person p where p.state=? and p.personNature!=? and p.gender=? and p.street=? and p.generalDict=?")
                    .setParameter(0, BaseEnum.StateEnum.Enable)
                    .setParameter(1, beiXuan)
                    .setParameter(2, gender)
                    .setParameter(3, street)
                    .setParameter(4, general)
                    .uniqueResult();
            return count!=null ? Integer.parseInt(count.toString()) : 0;
        }
    }

    @Override
    public List<Person> getHuanJieCountByStreet(DictItem street, DictItem tuixiufanpin, DictItem general) {
        List<Person> personList = getSession().createQuery("from Person p where p.state=? and p.street=? and p.personNature=? and p.generalDict=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, street)
                .setParameter(2, tuixiufanpin)
                .setParameter(3, general)
                .list();
        return personList;
    }

    @Override
    public Person getByKeyId(String keyId) {
        List<Person> personList = getSession().createQuery("from Person p where p.keyId=?")
                .setParameter(0, keyId)
                .list();
        if(personList.size()>0){
            return personList.get(0);
        }else{
            return null;
        }
    }

}
